﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MenuItemExtensions.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the MenuItemExtensions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Windows.RibbonWrapper
{
    using System;
    using System.Collections.Generic;
    using System.Windows;
    using Microsoft.Windows.Controls.Ribbon;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Windows.RibbonWrapper.MenuItemExtensions"/> type.
    /// </summary>
    public class MenuItemExtensions : DependencyObject
    {
        /// <summary>
        /// Element to group name property
        /// </summary>
        public static Dictionary<RibbonMenuItem, String> ElementToGroupNames = new Dictionary<RibbonMenuItem, String>();

        /// <summary>
        /// Dependency property of Group name
        /// </summary>
        public static readonly DependencyProperty GroupNameProperty = DependencyProperty.RegisterAttached(
            "GroupName",
            typeof(string),
            typeof(MenuItemExtensions),
            new PropertyMetadata(String.Empty, OnGroupNameChanged));

        /// <summary>
        /// Sets the name of the group.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="value">The value.</param>
        public static void SetGroupName(RibbonMenuItem element, string value)
        {
            element.SetValue(GroupNameProperty, value);
        }

        /// <summary>
        /// Gets the name of the group.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <returns>Group name for given element</returns>
        public static string GetGroupName(RibbonMenuItem element)
        {
            return element.GetValue(GroupNameProperty).ToString();
        }

        /// <summary>
        /// Called when [group name changed].
        /// </summary>
        /// <param name="d">The dependency object</param>
        /// <param name="e">The <see cref="System.Windows.DependencyPropertyChangedEventArgs"/> instance containing the event data.</param>
        private static void OnGroupNameChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            // Add an entry to the group name collection
            var menuItem = d as RibbonMenuItem;
            if (menuItem != null)
            {
                string newGroupName = e.NewValue.ToString();
                string oldGroupName = e.OldValue.ToString();
                if (string.IsNullOrEmpty(newGroupName))
                {
                    // Removing the toggle button from grouping
                    RemoveCheckboxFromGrouping(menuItem);
                }
                else
                {
                    // Switching to a new group
                    if (newGroupName != oldGroupName)
                    {
                        if (!string.IsNullOrEmpty(oldGroupName))
                        {
                            // Remove the old group mapping
                            RemoveCheckboxFromGrouping(menuItem);
                        }

                        ElementToGroupNames.Add(menuItem, e.NewValue.ToString());
                        menuItem.Checked += MenuItemChecked;
                    }
                }
            }
        }

        /// <summary>
        /// Removes the checkbox from grouping.
        /// </summary>
        /// <param name="checkBox">The check box.</param>
        private static void RemoveCheckboxFromGrouping(RibbonMenuItem checkBox)
        {
            ElementToGroupNames.Remove(checkBox);
            checkBox.Checked -= MenuItemChecked;
        }

        /// <summary>
        /// Menus the item checked.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private static void MenuItemChecked(object sender, RoutedEventArgs e)
        {
            var menuItem = e.OriginalSource as RibbonMenuItem;
            foreach (var item in ElementToGroupNames)
            {
                if (item.Key != menuItem && item.Value == GetGroupName(menuItem))
                {
                    item.Key.IsChecked = false;
                }
            }
        }
    }
}